package stv5.handlers;

import stv5.Episode;
import stv5.Request;
import stv5.Response;
import stv5.Series;
import stv5.Settings;
import stv5.abstracted.RequestHandler;
import us.danlib.templating.Template;
import us.danlib.templating.Templator;

/**
 * This handles requests to the main page, which lists all series that
 *  are being tracked, and provides a link to the settings page. Series
 *  that have been watched more recently will have a nice, shiny background.
 *  
 * @author Daniel
 *
 */
public class IndexHandler extends RequestHandler {
	private Settings mySettings;
	private Templator myTemplator;
	public IndexHandler(Settings settings) {
		mySettings = settings;
		myTemplator = new Templator("template/index.tpl");		
	}
	
	@Override
	protected boolean handle(Request request, Response response) {		
		Template t = myTemplator.createTemplate("CONFIG_LINK", "settings/view/");
		
		double timingMax = 1.0;
		if (t.hasTemplateVar("TIMING_PERC_MAX")) {
			try {
				timingMax = Double.parseDouble( t.getTemplateVar("TIMING_PERC_MAX") );
			} catch (Exception e) {}
		}
		String oldColor = t.hasTemplateVar("OLD_COLOR") ? t.getTemplateVar("OLD_COLOR") : null;
		String newColor = t.hasTemplateVar("NEW_COLOR") ? t.getTemplateVar("NEW_COLOR") : null;			
		
		char newLetter, currLetter = '.';	
		Episode next;
		for (Series ser : mySettings.getSeries()) {
			next = mySettings.getNextFor(ser);
			
			// begin adding the class
			t.beginClass("series");			
			
			t.addClassVar("SERIES_LINK", "media/browse/?id=" + ser.getId());
			t.addClassVar("SERIES_NAME", ser.getName());
			if (mySettings.seriesIsDone(ser))
				t.addClassVar("IS_DONE", "1");
			else if (mySettings.seriesIsMissing(ser))
				t.addClassVar("IS_MISSING", "1");
			else if (mySettings.seriesIsRecent(ser))
				t.addClassVar("IS_RECENT", "1");			
			
			// go ahead and always add the timing color
			if (oldColor == null || newColor == null)
				t.addClassVar("TIMING_COLOR", mySettings.getTimingColor(ser));
			else
				t.addClassVar("TIMING_COLOR", mySettings.getTimingColor(ser, oldColor, newColor));
			t.addClassVar("TIMING_PERC", ""+(mySettings.getTimingPercent(ser)*timingMax));
		
			if (next != null) {
				t.addClassVar("NEXT_NAME", next.getName());
				t.addClassVar("NEXT_LINK", ser.getNextLink());
			}

			// draw the big letter, if we're at a new part of the alphabet
			newLetter = ser.getName().toUpperCase().charAt(0);
			if (currLetter != newLetter) {
				currLetter = newLetter;
				t.addClassVar("LETTER", ""+currLetter);
			}
			
			t.endClass();
		}

		response.setContentType("text/html");
		response.setBody( t.render() );
		return true;
	}
	
	/*
	protected String oldhandle(Request request) {

		StringBuffer buff = new StringBuffer();
		buff.append(getStandardHeaders());
		buff.append("<h3>Welcome to SeriesTracker v5!</h3>\n<p>");
		buff.append("<a href=\"settings/view/\"><i>Click here for configuration</i></a>\n</p>\n");
		buff.append("<ul class=\"series_list\">\n");
		
		// prepare some vars
		
		char newLetter, currLetter = '.';								
		for (Series ser : mySettings.getSeries()) {
			// draw the big letter, if we're at a new part of the alphabet
			newLetter = ser.getName().toUpperCase().charAt(0);
			if (currLetter != newLetter) {
				currLetter = newLetter;
				buff.append("<li class=\"big-letter\">" + newLetter + "</li>\n");
			}
			
			if (mySettings.seriesIsDone(ser)) {
				// give it the "done" class
				buff.append("<li class=\"series-done\">" +
						"<a href=\"media/browse/?id=" + ser.getId() +
						"\">"+ ser.getName() + "</a></li>\n");
			} else {
				// get the timing and make the background color 			
				buff.append("<li style=\"background-color:"+mySettings.getTimingColor(ser)
						+";\"><a href=\"media/browse/?id=" + ser.getId() 
						+"\">" + ser.getName() + "</a></li>\n");
			}
		}
		buff.append("</ul>\n");
		buff.append(getStandardFooters());
		return buff.toString();
	} */
}
